package com.sk.bilibili.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.dto.DiscussInfo;
import com.sk.bilibili.event.BiliBiliEvent;
import com.sk.bilibili.other.R;
import com.sk.bilibili.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import com.sk.bilibili.bean.Discuss;
import com.sk.bilibili.mapper.DiscussMapper;

import java.util.ArrayList;
import java.util.List;

/**
* @author 邵康
* @date 2021/11/25 10:15
*/
@Service
public class DiscussService extends ServiceImpl<DiscussMapper, Discuss> {

    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private PraiseService praiseService;
    @Autowired
    private TransformUtils transformUtils;
    @Autowired
    private ApplicationEventPublisher eventPublisher;

    public R<Discuss> add(Discuss discuss) {
        discuss=transformUtils.transform(discuss);
        int memberId = BSessionUtils.getMemberId();
        discuss.setMemberId(memberId);
        save(discuss);
        eventPublisher.publishEvent(new BiliBiliEvent<>(this,discuss));
        discuss.getExtra().addExtra(memberService.getById(memberId));
        discuss.setPraiseNum(0);
        discuss.getExtra().put("discusses",new ArrayList<Discuss>());
        handleReply(discuss);
        return R.success(discuss);
    }

    private void handleReply(Discuss discuss) {
        if(discuss.getType()!=3)return;
        Discuss target = getById(discuss.getTargetId());
        discuss.getExtra().put("reply",memberService.getById(target.getMemberId()));
    }

    public R<Void> delete(Integer id) {
        QueryWrapper<Discuss> queryWrapper = QueryWrapperUtils.eq("type", 3, "target_id", id);
        queryWrapper.select("id");
        list(queryWrapper).forEach(discuss -> delete(discuss.getId()));
        removeById(id);
        return R.success();
    }

    public R<Void> update(Discuss discuss) {
        updateById(discuss);
        return R.success();
    }

    public R<IPage<Discuss>> page(Integer page, Integer size) {
        return R.success(page(new Page<>(page,size),null));
    }

    public R<IPage<Discuss>> getDiscuss(DiscussInfo discussInfo) {
        int memberId = BSessionUtils.getMemberId();
        QueryWrapper<Discuss> queryWrapper = QueryWrapperUtils.eq(
                "type", discussInfo.getType(), "target_id", discussInfo.getTargetId());
        queryWrapper.orderByDesc(discussInfo.getColumn());
        IPage<Discuss> page = page(discussInfo.page(), queryWrapper);
        page.getRecords().forEach(discuss -> {
            List<Discuss> discusses=new ArrayList<>();
            warpDiscuss(discusses,discuss,memberId);
            discuss.getExtra().put("discusses",discusses);
        });
        return R.success(page);
    }


    public void warpDiscuss(List<Discuss> discusses, Discuss discuss, int memberId){
        Member member = memberService.getById(discuss.getMemberId());
        memberService.warpFocus(memberId,member);
        discuss.getExtra().addExtra(member);
        discuss.getExtra().put("praise",praiseService.hasPraise(memberId,3,discuss.getId()));
        QueryWrapper<Discuss> queryWrapper = QueryWrapperUtils.eq(
                "type", 3, "target_id", discuss.getId());
        List<Discuss> list = list(queryWrapper);
        list.forEach(son -> {
            son.getExtra().put("reply", member);
            warpDiscuss(discusses,son, memberId);
        });
        discusses.addAll(list);
    }

    public void changeNum(String field, int num, Integer id) {
        redisUtils.changeNum("Discuss-"+id,field,num);
    }

}
